<template>
  <view class="cate">
    <view class="cate_head">
      <my-search @searchClick="goSearch"></my-search>
    </view>
    <view class="cate_content">
      <!-- 左侧滚动区域 -->
      <scroll-view scroll-y="true" class="content_left" :style="{height:wh + 'px'}">
        <block v-for="(item,index) in cateList" :key="item.cat_id">
          <view :class="['cateList_item', activeFlag === index?'active':'']" @click="leftItem(index)">
            {{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧滚动区域 -->
      <scroll-view scroll-y="true" class="content_right" :style="{height:wh + 'px'}" :scroll-top="scrollTop">
        <block v-for="(item,index) in cateList2" :key="item.cat_id">
          <view class="catTitle">/ {{item.cat_name}} /</view>
          <view class="cate_imgSon">
            <view class="imgSon" v-for="itemSon in item.children" :key="itemSon.cat_id" @click="goodsList(itemSon)">
              <image :src="itemSon.cat_icon" mode="widthFix"></image>
              <text>{{itemSon.cat_name}}</text>
            </view>
          </view>
        </block>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import {
    cateGories
  } from '@/http/api.js'
  export default {
    data() {
      return {
        // 分类数据左侧
        cateList: [],
        // 分类数据右侧
        cateList2: [],
        // 窗口的可用高度 = 屏幕高度 — navigationBar高度 - tabBar高度
        wh: 0,
        activeFlag: 0,
        // 滚动条距离顶部的距离
        scrollTop: 0
      };
    },
    onLoad() {
      // 获取系统信息
      let sysInfo = uni.getSystemInfoSync();
      // 可使用窗口高度
      this.wh = sysInfo.windowHeight - 50
      this.getCateList()
    },
    methods: {
      // 获取分类数据
      async getCateList() {
        const {
          data: res
        } = await cateGories()
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        this.cateList2 = this.cateList[0].children
      },
      // 点击左侧导航栏
      leftItem(index) {
        this.activeFlag = index
        this.cateList2 = this.cateList[index].children
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      // 点击三级分类，跳转商品列表
      goodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      // 跳转到搜索页
      goSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .cate_content {
    display: flex;

    .content_left {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      width: 240rpx;
      text-align: center;

      .cateList_item {
        line-height: 120rpx;
        background-color: #f7f7f7;
        text-align: center;
        font-size: 24rpx;
      }

      .active {
        background-color: #fff;
        position: relative;

        &::before {
          content: '';
          display: block;
          width: 6rpx;
          height: 60rpx;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }

    .content_right {
      flex: 1;

      .catTitle {
        display: block;
        font-size: 24rpx;
        padding: 30rpx 0;
        text-align: center;
        font-weight: 700;
      }

      .cate_imgSon {
        display: flex;
        flex-wrap: wrap;

        .imgSon {
          width: 33.33%;
          margin-bottom: 20rpx;
          display: flex;
          flex-direction: column;
          align-items: center;

          image {
            width: 120rpx;
            height: 120rpx;
          }

          text {
            font-size: 24rpx;
          }
        }
      }

    }
  }
</style>
